<template>
	<view>
		<!-- 登录部分 -->
		<view class="wellcome">
			<view class="hydl">2、输入新密码</view>
			<view class="ysok">输入新密码，点击确定</view>

			<view class="num">新密码</view>
			<view class="unum">
				<input type="password" v-model="newpd1" value="" placeholder="请输入新密码" />
				<!-- <view class="psd" v-show="tips">长度为6-16位</view> -->
			</view>
			<view class="num">确认新密码</view>
			<view class="unum">
				<input type="password" v-model="newpd2" value="" placeholder="再次输入新密码" />
				<view class="psd" v-show="tips">两次密码不一致</view>
			</view>
			<button type="primary" @click="newpwd">确认</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				newpd1:'',
				newpd2:'',
				oldpwd:'',
				usertoken:'',
				tips:false,
				//验证规则
				rules: {
					newpd1: {
						rule: /^[0-9a-zA-Z]{6,16}$/,
						msg: "密码应该为6-16位"
					},
					newpd2: {
						rule: /^[0-9a-zA-Z]{6,16}$/,
						msg: "密码应该为6-16位"
					},
				}
			};
		},
		//接受到参数的页面旧密码页面
		onLoad(){
			let oldpwd = uni.getStorageSync("oldpwd")
			this.oldpwd= oldpwd
			console.log(this.oldpwd)  
			
			let usertoken = uni.getStorageSync("usertoken")
			this.usertoken = usertoken
			console.log(this.usertoken)  
		},
		methods: {
			//判断验证是否符合要求，合法性校验
			validate(key) {
				let bool = true;
				if (!this.rules[key].rule.test(this[key])) {
					//提示信息
					uni.showToast({
						title: this.rules[key].msg,
					})
		
					//取反
					bool = false;
					return false;
				}
				return bool;
			},
			//登录验证
			newpwd(e) {
				const _this = this;
				if (!this.validate('newpd2')) return;
				if (!this.validate("newpd2")) return;
				// if(){
					
				// }
				if (this.newpd1==this.newpd2) {
				uni.request({
					url: 'http://124.70.161.103:8080/system/user/profile/updatePwd?oldPassword='+this.oldpwd+'&newPassword='+this.newpd2,
					method: 'PUT',
					header: {
						// 'custom-header': 'hello' ,//自定义请求头信息,
						'Authorization' : this.usertoken
					},
					success: (res) => {
						console.log(res.data);
						if (res.data.code === 200) {
							uni.showLoading({
								title: "修改中..."
							});
							setTimeout(() => {
								//隐藏登录状态
								uni.hideLoading();
								uni.navigateTo({
									url: "../index/index",
								});
							}, 2000)
							// uni.setStorageSync("usertoken",res.data.token)
						}
						if (res.data.code === 500) {
							uni.showToast({
								icon: "error",
								title: "新密码不能与旧密码相同"
							})
							this.tips = false
							this.tipss = false
						}
					},
					fail: () => {
						uni.showToast({
							icon: "none",
							title: "网络异常，请稍后再试"
						})
					}
				});
				
					
				} else {
					uni.showToast({
						icon:"error",
						title:"两次密码不一致"
					})
				}
			},
			
			
		}
	}
</script>

<style lang="scss">
	* {
		// border: 1rpx solid;
	}


	/* 自定义导航栏 */
	

	.wellcome {
		padding: 70rpx;

		.hydl {
			font-size: 37rpx;
			font-weight: 600;
		}

		.ysok {
			font-size: 23rpx;
			margin-top: 20rpx;
			margin-bottom: 100rpx;
			color: #999999;
		}

		.num {
			margin-top: 30rpx;
			font-size: 28rpx;
		}
		
		.unum{
			display: flex;
			justify-content: space-between;
			border-bottom: 1px solid #e8e8e8;
				margin-top: 14rpx;
			.psd{
				color: red;
				font-size: 20rpx;
				height: 32rpx;
				padding: 10rpx;
			}
		}

		input {
			font-size: 28rpx;
			color: #4CD964;
			padding: 10rpx 0;
		}

		button {
			margin-top: 60rpx;
		}

		.forgetpwd {
			margin-top: 20rpx;
			font-size: 30rpx;
			float: right;
			color: #999999;
		}
	}
</style>
